/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package lt.egidijus.aatest.properties;

import lt.atgplugin.filters.LongestPathConstructorFilterImplTest;
import lt.egidijus.aatest.filters.LongestPathConstructorFilterImpl;
import lt.egidijus.aatest.filters.ShortestPathConstructorFilterImpl;
import lt.egidijus.aatest.utils.Constants;
import lt.egidijus.aatest.wizards.helpers.DefaultOptions;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.PropertyPage;

/**
 * Property page for "Android" project.
 * This is accessible from the Package Explorer when right clicking a project and choosing
 * "Properties".
 *
 */
public class AATestPropertyPage extends PropertyPage {

	private Text testProjectName;
	private Text testPostfixName;
	private Text projectName;
	private Text geneticPopulation;
	private Text testSuiteName;
	private Text testSuitePackage;
	private Button generateSetButton;
	private Button generateGetButton;
	private Button showTime;
	private Combo comboContructor;
	private Button comboGenetic;
	private Button generateRandom;
	
    public AATestPropertyPage() {

    }

    @Override
    protected Control createContents(Composite parent) {
    	GridData gridData = new GridData();
		gridData.horizontalSpan =1;
	//	gridData.horizontalAlignment = SWT.FILL;
	//	gridData.grabExcessHorizontalSpace = true;
		gridData.verticalAlignment = SWT.FILL;
	//	gridData.grabExcessVerticalSpace = true;
		
    	GridData gridData2 = new GridData();
    	gridData2.horizontalSpan = 1;
    	gridData2.horizontalAlignment = SWT.FILL;
    	gridData2.grabExcessHorizontalSpace = true;
    	gridData2.verticalAlignment = SWT.FILL;
    //	gridData2.grabExcessVerticalSpace = true;

		Composite container = new Composite(parent, SWT.NULL);

		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 2;
		layout.verticalSpacing = 2;
		layout.horizontalSpacing = 2;	
		
		
		Label labelProjectName = new Label(container, SWT.NULL);
		labelProjectName.setText("Source project name: ");
		projectName = new Text(container, SWT.FILL | SWT.BORDER);
		projectName.setTextLimit(30);
		projectName.setText(Constants.projectName );
		projectName.setLayoutData(gridData2);
		
		Label labelTestingProject = new Label(container, SWT.NULL);
		labelTestingProject.setText("Test project name: ");
		testProjectName = new Text(container, SWT.FILL | SWT.BORDER);
		testProjectName.setTextLimit(30);
		testProjectName.setText(Constants.testProjectName);
		testProjectName.setLayoutData(gridData2);	
		
		Label labelTestPostfixName = new Label(container, SWT.NULL);
		labelTestPostfixName.setText("Test posfix of class: ");
		testPostfixName = new Text(container, SWT.FILL | SWT.BORDER);
		testPostfixName.setTextLimit(30);
		testPostfixName.setText(Constants.testClassPostfix );
		testPostfixName.setLayoutData(gridData2);
		
		Label labeltestSuitePackage = new Label(container, SWT.NULL);
		labeltestSuitePackage.setText("Package name for test suite: ");
		testSuitePackage = new Text(container, SWT.FILL | SWT.BORDER);
		testSuitePackage.setTextLimit(30);
		testSuitePackage.setText(Constants.testSuitePackage );
		testSuitePackage.setLayoutData(gridData2);
			
		Label labeltestSuiteName = new Label(container, SWT.NULL);
		labeltestSuiteName.setText("Name for test suite: ");
		testSuiteName = new Text(container, SWT.FILL | SWT.BORDER);
		testSuiteName.setTextLimit(30);
		testSuiteName.setText(Constants.testSuiteClass );
		testSuiteName.setLayoutData(gridData2);
		
		Label labelGeneticPopuliation = new Label(container, SWT.NULL);
		labelGeneticPopuliation.setText("Population size in genetic method: ");
		geneticPopulation = new Text(container, SWT.FILL | SWT.BORDER);
		geneticPopulation.setTextLimit(5);
		geneticPopulation.setText(String.valueOf(Constants.geneticPopulation));
		geneticPopulation.setLayoutData(gridData);
		
		
		Label labelgenerateSetButton = new Label(container, SWT.NULL);
		labelgenerateSetButton.setText("Generate tests for set method: ");
		generateSetButton = new Button(container, SWT.CHECK);
		//generateSetButton.setText("Generate tests for set method");
		generateSetButton.setSelection(Constants.generateSetMethod);
		generateSetButton.setLayoutData(gridData2);
		
		Label labelgenerateGetButton = new Label(container, SWT.NULL);
		labelgenerateGetButton.setText("Generate tests for get method: ");
		generateGetButton = new Button(container, SWT.CHECK);
		generateGetButton.setSelection(Constants.generateGetMethod);
		generateGetButton.setLayoutData(gridData2);
		
		Label labelshowTime = new Label(container, SWT.NULL);
		labelshowTime.setText("Show execution time: ");
		showTime = new Button(container, SWT.CHECK);
		showTime.setSelection(Constants.showTimes);
		showTime.setLayoutData(gridData2);
		
		
		Label labelcomboContructor = new Label(container, SWT.NULL);
		labelcomboContructor.setText("Use constructor with parameter size: ");
		comboContructor = new Combo(container, SWT.VERTICAL|SWT.BORDER  |SWT.READ_ONLY);
		comboContructor.add("Min");
		comboContructor.add("Max");
		comboContructor.setLayoutData(gridData);
		comboContructor.select(0);
		
		Label labelcomboGenetic = new Label(container, SWT.NULL);
		labelcomboGenetic.setText("Genetic method use OCL: ");
		comboGenetic = new Button(container, SWT.CHECK);
		comboGenetic.setSelection(Constants.geneticUseOCL);
		comboGenetic.setLayoutData(gridData2);
		
		Label labelgenerateRandom = new Label(container, SWT.NULL);
		labelgenerateRandom.setText("If no OCL file use Random: ");
		generateRandom = new Button(container, SWT.CHECK);
		generateRandom.setSelection(Constants.noOCLGenerateRandom);
		generateRandom.setLayoutData(gridData2);
		
		return container;
    }

    @Override
    public boolean performOk() {
    	Constants.testProjectName = testProjectName.getText();
    	Constants.projectName = projectName.getText();
    	Constants.testClassPostfix = testPostfixName.getText();
    	Constants.testSuitePackage = testSuitePackage.getText();
    	Constants.testSuiteClass = testSuiteName.getText();
    	Constants.generateSetMethod = generateSetButton.getSelection();
    	Constants.generateGetMethod = generateGetButton.getSelection();
    	Constants.noOCLGenerateRandom = generateRandom.getSelection();
    	Constants.showTimes = showTime.getSelection();
    	if (!MatchNumbers(geneticPopulation.getText())){
    		this.setErrorMessage("ERROR!! Population size is not valid number");
    		return false;
    	}
    	Constants.constructorType = comboContructor.getItem(comboContructor.getSelectionIndex());
    	if (Constants.constructorType.equals("Min")){
    		DefaultOptions.setDefaultConstructor(new ShortestPathConstructorFilterImpl());
    	}
    	if (Constants.constructorType.equals("Max")){
    		DefaultOptions.setDefaultConstructor(new LongestPathConstructorFilterImpl());
    	}
    	
    	Constants.geneticUseOCL = comboGenetic.getSelection();
    	Constants.geneticPopulation = Integer.valueOf(geneticPopulation.getText());
    	this.setErrorMessage(null);
        return true;
    }
    
    static boolean MatchNumbers(String input){
        try{
           Integer.parseInt(input);
          }catch(NumberFormatException nfe){ return false; }
         return true;
    }
    



    @Override
    protected void performDefaults() {

    }

}
